<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      // 内部迭代器
      const each = function (ary, callback) {
        for (let i = 0, l = ary.length; i < l; i++) {
          callback.call(ary[i], i, ary[i]); // 把下标和元素当作参数传给 callback 函数
        }
      };
      each([1, 2, 3], function (i, n) {
        alert([i, n]);
      });

      function lastEach(arr, callback, objThis) {
        let curIndex = arr.length - 1;
        for (let i = curIndex; i >= 0; i--) {
          if (objThis && typeof objThis === 'object') {
            callback.call(objThis, ary[i], i); // 把下标和元素当作参数传给 callback 函数
          } else {
            callback.call(arr[i], ary[i], i); // 把下标和元素当作参数传给 callback 函数
          }
        }
      }
      // 外部迭代器
      function iterator(obj) {
        let current = 0;

        const next = () => {
          current += 1;
        };

        const isDone = () => {
          return current >= obj.length;
        };

        const getItem = () => {
          return obj[current];
        };

        return { next, isDone, getItem };
      }
    </script>
  </body>
</html>
